package com.yu.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.common.utils.PageUtils;
import com.yu.common.utils.Query;
import com.yu.gulimall.product.dao.CategoryBrandRelationDao;
import com.yu.gulimall.product.entity.BrandEntity;
import com.yu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.yu.gulimall.product.entity.CategoryEntity;
import com.yu.gulimall.product.service.BrandService;
import com.yu.gulimall.product.service.CategoryBrandRelationService;
import com.yu.gulimall.product.service.CategoryService;
import com.yu.gulimall.product.vo.BrandVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandService brandService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCateLogList(Long brandId) {

        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity byId = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(byId.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateCateLogDetail(@Param("catId") Long catId,@Param("categoryName") String categoryName) {
        categoryBrandRelationDao.updateCateInfo(catId,categoryName);
    }

    @Override
    public List<BrandVO> getCategoryBrandRelation(Long catId) {
        List<CategoryBrandRelationEntity> entities = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().
                eq("catelog_id", catId));
        return entities.stream().map(item -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandName(item.getBrandName());
            brandVO.setBrandId(item.getBrandId());
            return brandVO;
        }).collect(Collectors.toList());
    }

}